﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Ooganizer.Platform.Win32;

// Aliases
using CloserList = System.Collections.Generic.List<Ooganizer.Closer.ICloser>;

namespace Ooganizer.Closer
{
    public class CloserDispatch : ICloser
    {
        public CloserDispatch()
        {
            m_closers = new CloserList();

            InitializeClosers();
        }

        private bool InitializeClosers()
        {
            // Load closers
            m_closers.Add(new MSWordCloser());
            m_closers.Add(new WindowsCloser());  // Let generic windows closer try last

            return true;
        }

        public bool CloseApp(IntPtr hWnd, string location)
        {
            string processName = Win32Functions.GetProcessName(hWnd);

            // Find first closer that handles the process and use it
            foreach (var closer in m_closers)
            {
                ICloseable closeable = closer as ICloseable;

                if(closeable != null && closeable.CanClose(processName))
                {
                    return closer.CloseApp(hWnd, location);
                }
            }

            return true;
        }

        private CloserList m_closers;
    }
}
